﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Text;

namespace TrafficApp
{
    public class Crossing
    {

        /// <summary>
        /// the index number of the current crossing.
        /// </summary>
        public static int crossingNumber = 0;

        /// <summary>
        /// The stoplines at the various crossing
        /// Stopline is the line at which a car stops while waiting on a
        /// traffic light.
        /// </summary>
        public const int NORTH_Y1 = 60;
        public const int SOUTH_Y1 = 120;
        public const int NORTH_Y2 = 60; //todo change
        public const int SOUTH_Y2 = 120; //todo change
        public const int WEST_X1 = 65;
        public const int EAST_X1 = 125;
        public const int WEST_X2 = 60;
        public const int EAST_X2 = 115;

        public int CrossingNumber
        {
            get { return crossingNumber; }
        }

        /// <summary>
        /// this shows which light is currently active.
        /// in case of crossing type 1, this is not just
        /// one traffic light but two.
        /// </summary>
        private int presentLight = 0;

        /// <summary>
        /// list of pedestrians
        /// </summary>
        public List<Pedestrian> Pedestrians;
        public List<TrafficLight> Carlights = new List<TrafficLight>();

        /// <summary>
        /// list of Trafficlights
        /// </summary>
        public List<TrafficLight> PedesLights = new List<TrafficLight>();

        /// <summary>
        /// point of upperleft corner
        /// </summary>
        public Vector loc { get; set; }

        /// <summary>
        /// type of crossing (Cross1, Cross2,...)
        /// </summary>
        public string Type { get; set; }

        /// <summary>
        /// list of roads that exit the crossing
        /// </summary>
        public List<Road> Exits = new List<Road>();

        /// <summary>
        /// list of car generators
        /// </summary>
        public List<Garage> CarGenerators = new List<Garage>();

        /// <summary>
        /// constructor
        /// </summary>
        /// <param name="Loc">Location</param>
        /// <param name="type">type of crossing</param>
        public Crossing(Vector Loc, string type)
        {
            Pedestrians = new List<Pedestrian>();
            loc = Loc;
            crossingNumber++;
            Type = type;
            #region type = 1
            if (type == "Cross1")
            {
                CarTL CTL = new CarTL(new Vector((Loc.X + 41), (Loc.Y + 34)), 6, false); // left upper corner (left traffic light)
                Carlights.Add(CTL);
                CTL = new CarTL(new Vector((Loc.X + 47), (Loc.Y + 34)), 6, false); // left upper corner (right traffic light)
                Carlights.Add(CTL);
                CTL = new CarTL(new Vector((Loc.X + 128), (Loc.Y + 40)), 6, true); // right upper corner (upper traffic light)
                Carlights.Add(CTL);
                CTL = new CarTL(new Vector((Loc.X + 128), (Loc.Y + 46)), 6, true); // right upper corner (lower traffic light)
                Carlights.Add(CTL);
                CTL = new CarTL(new Vector((Loc.X + 128), (Loc.Y + 127)), 6, false); // right bottom corner (left traffic light)
                Carlights.Add(CTL);
                CTL = new CarTL(new Vector((Loc.X + 134), (Loc.Y + 127)), 6, false); // right bottom corner (right traffic light)
                Carlights.Add(CTL);
                CTL = new CarTL(new Vector((Loc.X + 35), (Loc.Y + 127)), 6, true); // left bottom corner (upper traffic light)
                Carlights.Add(CTL);
                CTL = new CarTL(new Vector((Loc.X + 35), (Loc.Y + 133)), 6, true); // left bottom corner (lower traffic light)
                Carlights.Add(CTL);
                Garage Gar = new Garage(new Vector(Loc.X + 65, Loc.Y + 0), 3); // upper road left side (going south)
                CarGenerators.Add(Gar);
                Gar = new Garage(new Vector(Loc.X + 86, Loc.Y + 0), 3); // upper road right side (going south)
                CarGenerators.Add(Gar);
                Gar = new Garage(new Vector(Loc.X + 179, Loc.Y + 65), 4); // right side road upper side (going west)
                CarGenerators.Add(Gar);
                Gar = new Garage(new Vector(Loc.X + 179, Loc.Y + 85), 4); // right side road lower side (going west)
                CarGenerators.Add(Gar);
                Gar = new Garage(new Vector(Loc.X + 86, Loc.Y + 179), 1); // lower side road left side (going north)
                CarGenerators.Add(Gar);
                Gar = new Garage(new Vector(Loc.X + 107, Loc.Y + 179), 1); // lower side road right side (going north)
                CarGenerators.Add(Gar);
                Gar = new Garage(new Vector(Loc.X + 0, Loc.Y + 85), 2); // left side road upper side (going east)
                CarGenerators.Add(Gar);
                Gar = new Garage(new Vector(Loc.X + 0, Loc.Y + 106), 2); // left side road lower side (going east)
                CarGenerators.Add(Gar);
            }
            #endregion
            #region type = 2
            if (type == "Cross2")
            {
                CarTL CTL = new CarTL(new Vector((Loc.X + 56), (Loc.Y + 6)), 6, false); // left upper corner (left traffic light)
                Carlights.Add(CTL);
                CTL = new CarTL(new Vector((Loc.X + 118), (Loc.Y + 40)), 6, true); // right upper corner (upper traffic light)
                Carlights.Add(CTL);
                CTL = new CarTL(new Vector((Loc.X + 118), (Loc.Y + 46)), 6, true); // right upper corner (lower traffic light)
                Carlights.Add(CTL);
                CTL = new CarTL(new Vector((Loc.X + 118), (Loc.Y + 155)), 6, false); // right bottom corner (left traffic light)
                Carlights.Add(CTL);
                CTL = new CarTL(new Vector((Loc.X + 44), (Loc.Y + 127)), 6, true); // left bottom corner (upper traffic light)
                Carlights.Add(CTL);
                CTL = new CarTL(new Vector((Loc.X + 44), (Loc.Y + 133)), 6, true); // left bottom corner (lower traffic light)
                Carlights.Add(CTL);
                PedesTL PTL = new PedesTL(new Vector((Loc.X + 50), (Loc.Y + 31)), 6); // left upper corner
                PedesLights.Add(PTL);
                PTL = new PedesTL(new Vector((Loc.X + 119), (Loc.Y + 31)), 6); // right upper corner
                PedesLights.Add(PTL);
                PTL = new PedesTL(new Vector((Loc.X + 118), (Loc.Y + 142)), 6); // right bottom corner
                PedesLights.Add(PTL);
                PTL = new PedesTL(new Vector((Loc.X + 50), (Loc.Y + 142)), 6); // left bottom corner
                PedesLights.Add(PTL);
                Garage Gar = new Garage(new Vector(Loc.X + 75, Loc.Y + 0), 3); // upper road left side (going south)
                CarGenerators.Add(Gar);
                Gar = new Garage(new Vector(Loc.X + 179, Loc.Y + 65), 4); // right side road upper side (going west)
                CarGenerators.Add(Gar);
                Gar = new Garage(new Vector(Loc.X + 179, Loc.Y + 85), 4); // right side road lower side (going west)
                CarGenerators.Add(Gar);
                Gar = new Garage(new Vector(Loc.X + 96, Loc.Y + 179), 1); // lower side road left side (going north)
                CarGenerators.Add(Gar);
                Gar = new Garage(new Vector(Loc.X + 0, Loc.Y + 85), 2); // left side road upper side (going east)
                CarGenerators.Add(Gar);
                Gar = new Garage(new Vector(Loc.X + 0, Loc.Y + 106), 2); // left side road lower side (going east)
                CarGenerators.Add(Gar);

                //Add pedestrians
                Pedestrians.Add(new Pedestrian((new Vector(Loc.X + 64, Loc.Y + 50)), (new Vector(2, 0))));
                Pedestrians.Add(new Pedestrian((new Vector(Loc.X + 63, Loc.Y + 53)), (new Vector(1, 0))));
                Pedestrians.Add(new Pedestrian((new Vector(Loc.X + 62, Loc.Y + 47)), (new Vector(0.5f, 0))));
            }
            #endregion
        }

        /// <summary>
        /// updates traffic light situation (should be placed in tmr_tick)
        /// </summary>
        public void TLRegulation(int Passed)
        {
            #region type = 1
            if (Type == "Cross1")
            {
                if (Passed % 90 == 0)
                {
                    presentLight++;
                    presentLight %= 4;
                }
                if (Passed % 90 >= 0 && Passed % 90 <= 50)
                {
                    switch (presentLight)
                    {
                        case 0:
                            Carlights[0].ChangeColor(TrafficLight.colour.Green);
                            Carlights[5].ChangeColor(TrafficLight.colour.Green);
                            break;

                        case 1:
                            Carlights[2].ChangeColor(TrafficLight.colour.Green);
                            Carlights[7].ChangeColor(TrafficLight.colour.Green);
                            break;

                        case 2:
                            Carlights[1].ChangeColor(TrafficLight.colour.Green);
                            Carlights[4].ChangeColor(TrafficLight.colour.Green);
                            break;

                        case 3:
                            Carlights[3].ChangeColor(TrafficLight.colour.Green);
                            Carlights[6].ChangeColor(TrafficLight.colour.Green);
                            break;
                    }
                }
                if (Passed % 90 >= 50 && Passed % 90 <= 80)
                {
                    switch (presentLight)
                    {
                        case 0:
                            Carlights[0].ChangeColor(TrafficLight.colour.Yellow);
                            Carlights[5].ChangeColor(TrafficLight.colour.Yellow);
                            break;

                        case 1:
                            Carlights[2].ChangeColor(TrafficLight.colour.Yellow);
                            Carlights[7].ChangeColor(TrafficLight.colour.Yellow);
                            break;

                        case 2:
                            Carlights[1].ChangeColor(TrafficLight.colour.Yellow);
                            Carlights[4].ChangeColor(TrafficLight.colour.Yellow);
                            break;

                        case 3:
                            Carlights[3].ChangeColor(TrafficLight.colour.Yellow);
                            Carlights[6].ChangeColor(TrafficLight.colour.Yellow);
                            break;
                    }
                }
                if (Passed % 90 >= 80)
                {
                    switch (presentLight)
                    {
                        case 0:
                            Carlights[0].ChangeColor(TrafficLight.colour.Red);
                            Carlights[5].ChangeColor(TrafficLight.colour.Red);
                            break;

                        case 1:
                            Carlights[2].ChangeColor(TrafficLight.colour.Red);
                            Carlights[7].ChangeColor(TrafficLight.colour.Red);
                            break;

                        case 2:
                            Carlights[1].ChangeColor(TrafficLight.colour.Red);
                            Carlights[4].ChangeColor(TrafficLight.colour.Red);
                            break;

                        case 3:
                            Carlights[3].ChangeColor(TrafficLight.colour.Red);
                            Carlights[6].ChangeColor(TrafficLight.colour.Red);
                            break;
                    }
                }
            }
            #endregion
            #region type = 2
            else
            {
                if (Passed % 150 >= 100 && Passed % 150 <= 130)
                {
                    switch (presentLight)
                    {
                        case 0:
                            Carlights[5].ChangeColor(TrafficLight.colour.Yellow);
                            Carlights[1].ChangeColor(TrafficLight.colour.Yellow);
                            break;

                        case 1:
                            Carlights[1].ChangeColor(TrafficLight.colour.Yellow);
                            break;

                        case 2:
                            Carlights[1].ChangeColor(TrafficLight.colour.Yellow);
                            break;

                        case 3:
                            Carlights[1].ChangeColor(TrafficLight.colour.Yellow);
                            Carlights[1].ChangeColor(TrafficLight.colour.Yellow);
                            break;

                        case 4:
                            break;
                    }
                }
                if (Passed % 150 >= 130 && Passed % 150 <= 0)
                {
                    switch (presentLight)
                    {
                        case 0:
                            Carlights[5].ChangeColor(TrafficLight.colour.Red);
                            Carlights[1].ChangeColor(TrafficLight.colour.Red);
                            break;

                        case 1:
                            Carlights[1].ChangeColor(TrafficLight.colour.Red);
                            break;

                        case 2:
                            Carlights[1].ChangeColor(TrafficLight.colour.Red);
                            break;

                        case 3:
                            Carlights[1].ChangeColor(TrafficLight.colour.Red);
                            Carlights[1].ChangeColor(TrafficLight.colour.Red);
                            break;

                        case 4:
                            foreach (PedesTL ptl in PedesLights)
                            {
                                ptl.ChangeColor(TrafficLight.colour.Red);
                            }
                            break;
                    }
                }
                if (Passed % 150 >= 0 && Passed % 150 <= 100)
                {
                    switch (presentLight)
                    {
                        case 0:
                            Carlights[5].ChangeColor(TrafficLight.colour.Green);
                            Carlights[1].ChangeColor(TrafficLight.colour.Green);
                            break;

                        case 1:
                            Carlights[1].ChangeColor(TrafficLight.colour.Green);
                            break;

                        case 2:
                            Carlights[1].ChangeColor(TrafficLight.colour.Green);
                            break;

                        case 3:
                            Carlights[1].ChangeColor(TrafficLight.colour.Green);
                            Carlights[1].ChangeColor(TrafficLight.colour.Green);
                            break;

                        case 4:
                            foreach (PedesTL ptl in PedesLights)
                            {
                                ptl.ChangeColor(TrafficLight.colour.Green);
                            }
                            break;
                    }
                }
                if (Passed % 150 == 0)
                {
                    presentLight++;
                    presentLight %= 5;
                }
            }
            #endregion
        }

        /// <summary>
        /// creates pedestrians
        /// </summary>
        public void PedestrianGen(Vector Loc, bool left)
        {
            int temp = App.r.Next(20);
            if (temp % 5 == 3)
            {
                if (left)
                {
                    Pedestrians.Add(new Pedestrian(Loc, new Vector(-1, 0)));
                }
                else
                {
                    Pedestrians.Add(new Pedestrian(Loc, new Vector(1, 0)));
                }
            }
        }

        public void TLLocChanger(List<TrafficLight> List, Vector newLoc)
        {
            foreach (TrafficLight tl in List)
            {
                tl.LocChanger(tl.horizon);
            }
        }

        ///// <summary>
        ///// Changes the location when dragging a trafficlight
        ///// </summary>
        //public void changeLocComps(string type, Vector Loc)
        //{
        //    #region type = 1
        //    if (type == "Cross1")
        //    {
        //        Carlights[0].Loc[0] = new Vector((Loc.X + 41), (Loc.Y + 34)); // left upper corner (left traffic light)
        //        Carlights[0].LocChanger(false);
        //        Carlights[1].Loc[0] = new Vector((Loc.X + 47), (Loc.Y + 34)); // left upper corner (right traffic light)
        //        Carlights[1].LocChanger(false);
        //        Carlights[2].Loc[0] = new Vector((Loc.X + 128), (Loc.Y + 40)); // right upper corner (upper traffic light)
        //        Carlights[2].LocChanger(true);
        //        Carlights[3].Loc[0] = new Vector((Loc.X + 128), (Loc.Y + 46)); // right upper corner (lower traffic light)
        //        Carlights[3].LocChanger(true);
        //        Carlights[4].Loc[0] = new Vector((Loc.X + 128), (Loc.Y + 127)); // right bottom corner (left traffic light)
        //        Carlights[4].LocChanger(false);
        //        Carlights[5].Loc[0] = new Vector((Loc.X + 134), (Loc.Y + 127)); // right bottom corner (right traffic light)
        //        Carlights[5].LocChanger(false);
        //        Carlights[6].Loc[0] = new Vector((Loc.X + 35), (Loc.Y + 127)); // left bottom corner (upper traffic light)
        //        Carlights[6].LocChanger(true);
        //        Carlights[7].Loc[0] = new Vector((Loc.X + 35), (Loc.Y + 133)); // left bottom corner (lower traffic light)
        //        Carlights[7].LocChanger(true);


        //        CarGenerators[0].location = new Vector(Loc.X + 65, Loc.Y + 0); // upper road left side (going south)
        //        CarGenerators[1].location = new Vector(Loc.X + 86, Loc.Y + 0); // upper road right side (going south)
        //        CarGenerators[2].location = new Vector(Loc.X + 179, Loc.Y + 65); // right side road upper side (going west)
        //        CarGenerators[3].location = new Vector(Loc.X + 179, Loc.Y + 85); // right side road lower side (going west)
        //        CarGenerators[4].location = new Vector(Loc.X + 86, Loc.Y + 179); // lower side road left side (going north)
        //        CarGenerators[5].location = new Vector(Loc.X + 107, Loc.Y + 179); // lower side road right side (going north)
        //        CarGenerators[6].location = new Vector(Loc.X + 0, Loc.Y + 85); // left side road upper side (going east)
        //        CarGenerators[7].location = new Vector(Loc.X + 0, Loc.Y + 106); // left side road lower side (going east)
        //    }
        //    #endregion
        //    #region type = 2
        //    if (type == "Cross2")
        //    {
        //        Carlights[0].Loc[0] = new Vector((Loc.X + 41), (Loc.Y + 34)); // left upper corner (left traffic light)
        //        Carlights[0].LocChanger(false);
        //        Carlights[1].Loc[0] = new Vector((Loc.X + 56), (Loc.Y + 6)); // left upper corner (left traffic light)
        //        Carlights[1].LocChanger(true);
        //        Carlights[2].Loc[0] = new Vector((Loc.X + 118), (Loc.Y + 40)); // right upper corner (upper traffic light)
        //        Carlights[2].LocChanger(true);
        //        Carlights[3].Loc[0] = new Vector((Loc.X + 118), (Loc.Y + 46)); // right upper corner (lower traffic light)
        //        Carlights[3].LocChanger(false);
        //        Carlights[4].Loc[0] = new Vector((Loc.X + 118), (Loc.Y + 155)); // right bottom corner (left traffic light)
        //        Carlights[4].LocChanger(true);
        //        Carlights[5].Loc[0] = new Vector((Loc.X + 44), (Loc.Y + 127)); // left bottom corner (upper traffic light)
        //        Carlights[5].LocChanger(true);
        //        Carlights[6].Loc[0] = new Vector((Loc.X + 44), (Loc.Y + 133)); // left bottom corner (lower traffic light)
        //        Carlights[6].LocChanger(true);


        //        PedesLights[0].Loc[0] = new Vector((Loc.X + 50), (Loc.Y + 31)); // left upper corner
        //        PedesLights[0].LocChanger(true);
        //        PedesLights[1].Loc[0] = new Vector((Loc.X + 119), (Loc.Y + 31)); // right upper corner
        //        PedesLights[1].LocChanger(true);
        //        PedesLights[2].Loc[0] = new Vector((Loc.X + 118), (Loc.Y + 142)); // right bottom corner
        //        PedesLights[2].LocChanger(true);
        //        PedesLights[3].Loc[0] = new Vector((Loc.X + 50), (Loc.Y + 142)); // left bottom corner
        //        PedesLights[3].LocChanger(true);


        //        CarGenerators[0].location = new Vector(Loc.X + 75, Loc.Y); // upper road left side (going south)
        //        CarGenerators[1].location = new Vector(Loc.X + 179, Loc.Y + 65); // right side road upper side (going west)
        //        CarGenerators[2].location = new Vector(Loc.X + 179, Loc.Y + 85); // right side road lower side (going west)
        //        CarGenerators[3].location = new Vector(Loc.X + 96, Loc.Y + 179); // lower side road left side (going north)
        //        CarGenerators[4].location = new Vector(Loc.X + 0, Loc.Y + 85); // left side road upper side (going east)
        //        CarGenerators[5].location = new Vector(Loc.X + 0, Loc.Y + 106); // left side road lower side (going east)
        //    }
        //    #endregion

    }
}
